/*************************************************
 *FileName:      messages.js
 *Description:   系统消息.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init({
        gestureConfig:{
           longtap: true, //默认为false
        } 
    });
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var page = 1,page_num=20;
    var isUp = true,elem;
    var otime , oldtime=null;
    /*
     * HTML5+准备完成
     */
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //消息加载
        getmsg();
    });
    
    $('.messages').on('tap','.helplink',function(){
        common.pageTransition('help.html')
    })
    /**
     * getmsg
     * 消息加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function getmsg(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/notice/noticesList",
            dataType:'jsonp',
            data: {uid:uid,token:token,page:page,page_num:page_num},
            success: function(data) {
                isUp = false;
                if(data.error_code == 0){
                    elem =  $('.main');
                    var ohtml='';
                    for(var i=data.data.length-1 ; i>=0; i--){
                        otime =chatubtime(data.data[i].send_time,oldtime);
                        oldtime=data.data[i].send_time;
                        ohtml +=otime+' <div class="chat-box chat-left"><img class="user" src="images/userindex.jpg"/>'
                                +'<div class="text-box"><div class="text"><em class="em"></em><div class="fz-text">'+data.data[i].content+'</div></div></div></div>';
                    }
                    $('.main').prepend(ohtml);
                    if(page==1){
                        $('.mui-content').scrollTop($('.main').height()) 
                    }else{
                        scrollTop = common.getTop($('#loging')[0]);
                        $('.mui-content').scrollTop(scrollTop);
                    }
                    $('.main').children('#loging').remove();
                    page++;
                    isUp = true;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    $('.main').find('#loging').text('已经到顶了').addClass('ovr');
                }
            }  
        });
}
    
    $('.mui-content').on('scroll',function(){//监听滚动
        var scroll_top = $('.mui-content').scrollTop();
        var doc_height = $('.main').height();
        if(scroll_top <= 0 && isUp==true){
            $('.main').prepend('<div id="loging"></div>')
            setTimeout(function(){getmsg()},150)
            isUp = false;
        }
    })
    
    $('.messages').on('longtap','.text-box',function(){
        $('#topPopover').addClass('videobox2');
        $('.mui-table-view-cell').html('<a class="copy">复制</a>');
        $(this).attr('id','openPopover');
        mui('.mui-popover').popover('toggle',document.getElementById("openPopover"));
    })//弹出复制等按钮
    
    $('.messages').on('tap','.mui-backdrop',function(){//删除复制id
        $('#openPopover').removeAttr('id');
    })
    
    $('.mui-scroll-wrapper').on('tap','.copy',function(){
        var txt = $('#openPopover .fz-text').text();
        setClipbordText(txt);
        mui('.mui-popover').popover('toggle');//show hide toggle
        $('#openPopover').removeAttr('id');
    })//点击复制
    
    function setClipbordText(txt) {
        if(!window.plus) return;
        if(mui.os.android) {
            var Context = plus.android.importClass("android.content.Context");
            var main = plus.android.runtimeMainActivity();
            var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
            plus.android.invoke(clip,"setText",txt);
        } else {
            var UIPasteboard  = plus.ios.importClass("UIPasteboard");
            var generalPasteboard = UIPasteboard.generalPasteboard();
            generalPasteboard.setValueforPasteboardType(txt,"public.utf8-plain-text");
        }
    }
    
    function getClipbordText() {
        if(!window.plus) return;
        if(mui.os.android) {
            var Context = plus.android.importClass("android.content.Context");
            var main = plus.android.runtimeMainActivity();
            var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
            return plus.android.invoke(clip, "getText");
        } else {
            var UIPasteboard = plus.ios.importClass("UIPasteboard");
            var _val=generalPasteboard.plusCallMethod({valueForPasteboardType:"public.utf8-plain-text"});
            return _val || '';
        }
    }
    